// pages/userinfor/userinfor.js
const app = getApp() // getApp()获取App上的数据

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    head_Photo: '',
    canIUseOpenData:wx.canIUse('open-data.type.userNickName'),
    sex_value: wx.getStorageSync('user').sex ||'男',
    age_value: wx.getStorageSync('user').age || 0,
    userName_value:wx.getStorageSync('user').userName
  },

  /**
   * 生命周期函数--监听页面加载
   */
  user:null,
  onLoad: function (options) {
    let id = wx.getStorageSync('user').userId;
    this.user=wx.getStorageSync('user')
    console.log(this.user)

    this.setData({
      sex_value: this.user.sex || '男',
      age_value: this.user.age || 0,
      userName_value: this.user.userName
    })
  },
  
  
  //性别
  sex: function(e){
    console.log(e)
    this.setData({
      sex_value: e.detail.value
    })
  },
  //年龄
  age: function(e){
    this.setData({
      age_value: e.detail.value
    })
  },
  //保存修改
  updateInfor: function(){
    // let id = wx.getStorageSync('user').userId;
    wx.request({
      url: `${app.globalData.url}/customer/user/${this.user.userId}`,
      method:"PUT",
      dataType:"text",
      header: {
        'content-type':'application/json'
      },
      data:{
        userName: this.data.userName_value,
        sex: this.data.sex_value,
        age: parseInt(this.data.age_value)
      },
      success:(res)=>{
        console.log(res.data)
      },
      fail:(err)=>{
        console.log(err)
      },
      
      // setData:{
      //   userName: this.data.userName_value
      // }
    })
    
    // 本地存储成为user对象
    wx.setStorageSync('user', {
      userId: wx.getStorageSync('user').userId,
      userName: this.data.userName_value,
      sex: this.data.sex_value,
      age: parseInt(this.data.age_value),
      head_Photo: wx.getStorageSync('user').head_Photo
    })
    console.log(wx.getStorageSync('user'))

    this.setData({
      sex_value: this.data.sex_value,
      age_value: parseInt(this.data.age_value)
    })
    wx.navigateBack({
      delta: 1,
    })
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})